using HCore.Unity;
using HCore.Unity.Core;
using HCore.Unity.Extension;
using UnityEngine;
using UnityEngine.EventSystems;

namespace HCore.Tests.Runtime.Tilemap
{
    public class TilemapHandle : MonoBehaviour
    {
        [SerializeField] private TilemapVisual tilemapVisual;
        private Tilemap mTilemap;
        private TilemapSprite mTilemapSprite = TilemapSprite.None;

        private void Awake()
        {
            var transform1 = GameUtils.MainCamera.transform;
            var position = transform1.position;
            position.x = 100;
            position.y = 50;
            transform1.position = position;

            mTilemap = new Tilemap(20, 10, 10f, Vector2.zero);
        }

        // Start is called before the first frame update
        private void Start()
        {
            mTilemap.SetTilemapVisual(tilemapVisual);

            GameDebug.ButtonUI(new Vector2(100, 370), "保存", () => mTilemap.Save($"{HPaths.PATH_STREAMING_ASSETS_FOLDER}/tilemap.json"));
            GameDebug.ButtonUI(new Vector2(100, 300), "读取", () => mTilemap.Load($"{HPaths.PATH_STREAMING_ASSETS_FOLDER}/tilemap.json"));
            GameDebug.ButtonUI(new Vector2(100, 230), "清除", () => mTilemapSprite = TilemapSprite.None);
            GameDebug.ButtonUI(new Vector2(100, 160), "地板", () => mTilemapSprite = TilemapSprite.Ground);
            GameDebug.ButtonUI(new Vector2(100, 90), "路径", () => mTilemapSprite = TilemapSprite.Path);
            GameDebug.ButtonUI(new Vector2(100, 20), "泥土", () => mTilemapSprite = TilemapSprite.Dirt);
        }

        // Update is called once per frame
        private void Update()
        {
            if (EventSystem.current.IsPointerOverGameObject()) return;

            if (Input.GetMouseButton(0))
            {
                var mouseWorldPosition = GameUtils.GetWorldPositionFromScreen().ToVector2();
                mTilemap.SetTilemapSprite(mouseWorldPosition, mTilemapSprite);
            }
        }
    }
}